package com.pilipili.zeus.eduService.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduCourse;
import com.pilipili.zeus.eduService.entity.excel.CourseExcelData;
import com.pilipili.zeus.eduService.service.EduCourseService;

public class CourseExcelListener extends AnalysisEventListener<CourseExcelData> {
    public EduCourseService courseService;

    public CourseExcelListener() {
    }

    public CourseExcelListener(EduCourseService courseService) {
        this.courseService = courseService;
    }
    //按行读取Excel内容
    @Override
    public void invoke(CourseExcelData courseExcelData, AnalysisContext analysisContext) {
        if(courseExcelData ==null){
            throw new MyException(20001,"文件数据为空");
        }
        //查询数据(name,0)是否存在,不存在则将数据写入数据库
        EduCourse existCourse = this.existCourse(courseService, courseExcelData.getId(),courseExcelData.getSubjectParentId());
        if(existCourse==null){
            existCourse=new EduCourse();
            existCourse.setId(courseExcelData.getId());
            existCourse.setTitle(courseExcelData.getTitle());
            existCourse.setCover(courseExcelData.getCover());
            existCourse.setLessonNum(courseExcelData.getLessonNum());
            existCourse.setSubjectParentId(courseExcelData.getSubjectParentId());
            courseService.save(existCourse);
        }
//        //查询数据(name,1)是否存在,不存在则将二级标题写入数据库
//        String id = existOneSubject.getId();
//        EduSubject existTwoSubject = this.existTwoSubject(courseService, courseExcelData.getTwoSubjectName(),id);
//        if(existTwoSubject==null){
//            existTwoSubject=new EduSubject();
//            existTwoSubject.setParentId(id);
//            existTwoSubject.setTitle(courseExcelData.getTwoSubjectName());
//            courseService.save(existTwoSubject);
//        }
    }
    private EduCourse existCourse(EduCourseService courseService, String id,String subjectParentId){
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq("subject_parent_id",subjectParentId);
        return courseService.getOne(wrapper);
    }
//    private EduSubject existTwoSubject(EduSubjectService subjectService,String name,String id){
//        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
//        wrapper.eq("title",name);
//        wrapper.eq("subject_id",id);
//        return subjectService.getOne(wrapper);
//    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
